//google.load("jquery", "1.3.2");
//google.load("jqueryui", "1.7.2");

var sTool = {
	
	updateTasksCount: function(projectId, elem) {
		var url = "/tasksCount?projectId=" + projectId ;
		$.getJSON(url, function (data) {
	        if (data) {
	        	elem.innerHTML = data.count ;
	        }
		});

	},
		
	updateProjects: function(data) {
		if (data) {
			var nameId = "#view-" + data.id ;
			$(nameId).html(data.name) ;
			
			$("#dialog").empty() ;
			$("#dialog").dialog('destroy') ;
		}
	},

    addProject: function(data) {
        if (data) {

            var projectsTable = $("#projects-table") ;


            var projectRow = $("#sample-project-row tr").clone() ;
            projectRow.css('display', 'table-row') ;
            projectRow.find('#project-row-name').text(data.name) ;

            var settingsCell = projectRow.find('#project-row-edit') ;
            settingsCell.attr('id', 'edit-' + data.id) ;
            settingsCell.click(
                    function(e) {
                        sTool.clickLoadProjectSettings(e) ;
                    }
                    ) ;

            var nameCell = projectRow.find('#project-row-name') ;
            nameCell.attr('id', 'view-' + data.id) ;
            nameCell.click(
                    function(e) {
                        sTool.clickLoadProjectInfo(e) ;
                    }
                    ) ;

            var countCell = projectRow.find('#project-row-tasks') ;
            countCell.text('0') ;
            countCell.attr('id', 'count-' + data.id) ;


            projectsTable.append(projectRow) ;

            $("#dialog").empty() ;
            $("#dialog").dialog('destroy') ;

        }
    },

    addTask: function(data) {
        if (data.success == 0) {
            $("#dialog #create-task-message").text("Error while creating task") ;
        } else {

            var tasksTable = $("#tasks-table") ;

            var taskRow = $("#sample-task-row-table tr").clone() ;
            taskRow.find('#sample-task-index').text(data.index) ;
            taskRow.find('#sample-task-name').text(data.name) ;
            taskRow.find('#sample-task-createdAt').text(data.createdAt) ;
            taskRow.find('#sample-task-createdBy').text(data.createdBy) ;
            taskRow.find('#sample-task-assignedTo').text(data.assignedTo) ;
            taskRow.find('#sample-task-priority').text(data.priority) ;
            taskRow.find('#sample-task-status').text(data.status) ;

            tasksTable.append(taskRow) ;

            //        alert(data.projectId) ;
            //        alert($('#count-' + data.projectId).text()) ;
            var count = parseInt($('#count-' + data.projectId).text()) ;
            count += 1 ;
            $('#count-' + data.projectId).text(count) ;

            $("#dialog").empty() ;
            $("#dialog").dialog('destroy') ;
        }


    }, 

    fillTasks: function(data) {
        var tasksTable = $("#tasks-table") ;
        var createTaskLinkDiv = $('#create-task-link-div') ;
        tasksTable.find('tbody').empty();
        createTaskLinkDiv.empty() ;
        
        for (var i = 0 ; i < data.tasks.length; i ++) {
            var taskRow = $("#sample-task-row-table tr").clone() ;
            taskRow.find('#sample-task-index').text(data.tasks[i].index) ;
            taskRow.find('#sample-task-name').text(data.tasks[i].name) ;
            taskRow.find('#sample-task-createdAt').text(data.tasks[i].createdAt) ;
            taskRow.find('#sample-task-createdBy').text(data.tasks[i].createdBy) ;
            taskRow.find('#sample-task-assignedTo').text(data.tasks[i].assignedTo) ;
            taskRow.find('#sample-task-priority').text(data.tasks[i].priority) ;
            taskRow.find('#sample-task-status').text(data.tasks[i].status) ;
            taskRow.find('#sample-task-edit').text('edit') ;

            tasksTable.append(taskRow) ;
        }

        var createTaskLink = $('.create-task-link').clone() ;
        createTaskLink.css('display', 'inline') ;
        createTaskLink.attr('id', 'create-task-' + data.projectId) ;
        createTaskLink.click(function(e){
            sTool.showCreateTaskDialog({'projectId': data.projectId}) ;
        }) ;

        createTaskLinkDiv.append(createTaskLink) ;

    },

    clickCreateProjectButton: function(e)  {
        var projectName = $("#dialog #create-project-name").attr("value") ;
        var projectDescr = $("#dialog #create-project-descr").attr("value") ;

        if (projectName.trim() == '') {
            $("#dialog #create-project-name-message").text("Project name is empty") ;

//            alert("Project name is empty!")
        } else {
            $.post("/createProjectJSON",
            {name: projectName, description: projectDescr},
                    function(data) {
                        sTool.addProject(data) ;
                    }, "json"

                    ) ;
        }

    },

    clickCreateTaskButton: function(e) {
        $("#dialog #create-task-name-message").text("") ;
        $("#dialog #create-task-assignedTo-message").text("") ;


        var projectId = $('.create-task-projectId').attr('value') ;
        var taskName = $('#dialog #create-task-name').attr('value') ;
        //alert(taskName)
        var taskDescr = $('#dialog #create-task-description').attr('value') ;
        var taskAssignedTo = $('#dialog #create-task-assignedTo').attr('value') ;
        var taskPriority = $('#dialog #create-task-priority').attr('value') ;
        //alert(taskAssignedTo) ;

        if (taskName.trim() == '') {
            $("#dialog #create-task-name-message").text("Task name is required") ;
        } else if (taskAssignedTo.trim() == '') {
            $("#dialog #create-task-assignedTo-message").text("Assigned to is required") ;
        } else {

            $.post("/createTaskJSON",
            {
                projectId: projectId,
                name: taskName,
                description: taskDescr,
                assignedTo: taskAssignedTo,
                priority: taskPriority
            },
                    function(data) {
                        sTool.addTask(data) ;
                    }, "json"

                    ) ;
        }


    },
	
	clickUpdateProjectButton: function(e) {
		var projectName = $("#dialog #edit-project-name").attr("value") ;
		var projectId = $("#dialog #edit-project-id").attr("value") ;
		var projectDescr = $("#dialog #edit-project-descr").attr("value") ;

        if (projectName.trim() == '') {
            $('#dialog #edit-project-name-message').text('Project name is empty') ;
        } else {
            $.post("/updateProjectJSON",
            {projectId: projectId, projectName: projectName, description: projectDescr},
                    function(data) {
                        sTool.updateProjects(data) ;
                    }, "json"
                    ) ;
        }


	},
	
	showProjectSettingsDialog: function(data) {
		if (data) {
			$("#dialog").dialog({
					title: 'Project details',
                    width: 400,
                    height: 250,
					open:  function(event, ui) {

                        var settingsData = $('#project-settings').clone() ;
                        settingsData.css('display', 'block') ;

                        $('#dialog').append(settingsData) ;

                        settingsData.find('#edit-project-id').attr('value', data.id) ;
                        settingsData.find('#edit-project-name').attr('value', data.name) ;
                        settingsData.find('#edit-project-descr').attr('value', data.description) ;
                        settingsData.find('#edit-createdBy').text(data.createdBy) ;

						settingsData.find("#edit-save-project").click(
							function(e) {
								sTool.clickUpdateProjectButton(e) ;
							}
						) ;
					}, 
					close : function(event, ui) {
						$("#dialog").empty() ;
						$("#dialog").dialog('destroy') ;
					}
					
				}
			) ;
			
		}
	},
	
	showProjectInfoDialog: function(data) {
		if (data) {
			$("#dialog").dialog({
					title: 'Project details', 
					open:  function(event, ui) {

                        var tableData = $('#project-info-table').clone() ;
                        tableData.css('display', 'block') ;

                        $('#dialog').append(tableData) ;

                        tableData.find("#project-name").text(data.name) ;
                        tableData.find("#project-descr").text(data.description) ;
                        tableData.find("#project-createdBy").text(data.createdBy) ;

					},
					close : function(event, ui) {
						$("#dialog").empty() ;
						$("#dialog").dialog('destroy') ;
					}
					
				}
			) ;
			
		}
	},

    showCreateProjectDialog: function() {
        $("#dialog").dialog({
                title: 'Add project',
                width: 400,
                height: 250,
                open:  function(event, ui) {

                    var tableData = $('#create-project').clone() ;
                    tableData.css('display', 'block') ;

                    $('#dialog').append(tableData) ;

                    tableData.find('#create-project-button').click(
                            function(e) {
                                sTool.clickCreateProjectButton(e)
                            }
                            );
                },
                close : function(event, ui) {
                    $("#dialog").empty() ;
                    $("#dialog").dialog('destroy') ;
                }

            }
        ) ;
    },
    showCreateTaskDialog: function(data) {
        $("#dialog").dialog({
                title: 'Add task',
                width: 700,
                height: 300,
                open:  function(event, ui) {

                    var taskData = $('#create-task').clone() ;
                    taskData.css('display', 'block') ;
                    taskData.find('#create-task-projectId').attr('value', data.projectId) ;
                    taskData.find('#create-task-projectId').addClass('create-task-projectId') ;
                    $('#dialog').append(taskData) ;

                    taskData.find('#create-task-save').click(
                            function(e) {
                                sTool.clickCreateTaskButton(e)
                            }
                            );
                },
                close : function(event, ui) {
                    $("#dialog").empty() ;
                    $("#dialog").dialog('destroy') ;
                }

            }
        ) ;
    },
	
	clickLoadProjectSettings :  function(e) {
		e.preventDefault() ;
		var tmpId = e.target.id ;
		var projectId = tmpId.substring(5, tmpId.length) ;
		$.getJSON("/projectEditDetailsJSON?projectId=" + projectId,
			function(data) {
				sTool.showProjectSettingsDialog(data) ;
			}
		) ;
	}, 
	
	clickLoadProjectInfo: function(e) {
		e.preventDefault() ;
		var tmpId = e.target.id ;
		var projectId = tmpId.substring(5, tmpId.length) ;
		$.getJSON("/projectDetailsJSON?projectId=" + projectId, 
			function(data) {
				sTool.showProjectInfoDialog(data) ;
			}
		); 
	
	},

    clickLoadProjectTasks: function(e) {
        e.preventDefault() ;
        var tmpId = e.target.id ;
        var projectId = tmpId.substring(5, tmpId.length) ;
        $.getJSON("/tasksJSON?projectId=" + projectId,
            function(data) {
                sTool.fillTasks(data) ;
            }
        );
    }
} ;
